package likouJava;

public class 搜索二维矩阵2 {
    public boolean searchMatrix(int[][] matrix, int target) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int m = matrix.length;
        int n = matrix[0].length;

        int top = 0, left = 0;
        int right = n - 1, bottom = m - 1;
        //由于矩阵从左到右从上到下都是升序，故可从右上或者左下开始查询

        while (top <= bottom && right >= 0) {

            if (matrix[top][right] == target) {
                return true;
            } else if (target < matrix[top][right]) {
                //从右往左
                right--;
            } else {
                //从上往下
                top++;
            }
        }
        return false;
    }

}
